﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.ControlLib;
using xiaoxiong.KYJControllersLib;
using xiaoxiong.ScadaProModbusRtu.Common;

namespace xiaoxiong.ScadaProModbusRtu.childFrm
{
    public partial class FrmTapControll : Form
    {
        public FrmTapControll()
        {
            InitializeComponent();

            updateTimer = new Timer();
            updateTimer.Interval = 200;
            updateTimer.Tick += UpdateTimer_Tick;
            updateTimer.Start();

            this.FormClosing += (s, e) =>
            {
                updateTimer.Stop();
            };

        }

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            UpdateCommon(this);
        }

        private Timer updateTimer;

        private void UpdateCommon(Control parentControl)
        {
            if (CommonMethods.plcDevice != null && CommonMethods.plcDevice.IsConnected)
            {
                foreach (Control control in parentControl.Controls)
                {
                    //显示deviceControl数据
                    if (control is DeviceControl deviceControl)
                    {
                        if (deviceControl.StateAddress.Length > 0)
                        {
                            byte value = 0;
                            if (byte.TryParse(CommonMethods.plcDevice[deviceControl.StateAddress]?.ToString(), out value))
                            {
                                deviceControl.DeviceState = value;
                            }
                        }
                    }
                    else if (control.HasChildren)
                    {
                        UpdateCommon(control);
                    }
                }
            }
        }



        #region 无边框拖动

        //窗体拖动事件
        private bool _isDragging = false;
        private Point _startPoint = new Point(0, 0);
        private void TopPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _isDragging = true;
                _startPoint = new Point(e.X, e.Y);
            }
        }

        private void TopPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isDragging)
            {
                Point newPoint = this.PointToScreen(new Point(e.X, e.Y));
                this.Location = new Point(newPoint.X - _startPoint.X, newPoint.Y - _startPoint.Y);
            }
        }

        private void TopPanel_MouseUp(object sender, MouseEventArgs e)
        {
            _isDragging = false;
        }
        #endregion

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        
        private void deviceControl1_DeviceControlEvent(object sender, EventArgs e)
        {
            //KYJ1In_Open
            //KYJ1In_Close
            // 获取控件的标识（如 1 或 2）



            // 解析 sender 字符串
            string[] parts = sender.ToString().Split('_');
            if (parts.Length == 2)
            {
                string deviceName = parts[0]; // 如 "KYJ1"
                string action = parts[1];    // 如 "Open" 或 "Close"

                // 提取设备编号
                string deviceIndex = deviceName.Replace("KYJ", "").Replace("In", ""); // 如 "1"

                // 动态生成信号名称
                string openSignalName = $"KYJ{deviceIndex}In_Open";
                string closeSignalName = $"KYJ{deviceIndex}In_Close";

                string SignalState = $"KYJ{deviceIndex}In_State";
                // 根据操作类型执行操作
                if (action == "Open")
                {
                    // 开启操作
                    if (CommonMethods.CommonWrite(openSignalName, "true").IsSuccess)
                    {
                        CommonMethods.CommonWrite(closeSignalName, "false");
                        //同时写入控制阀
                        CommonMethods.CommonWrite(SignalState, "1");
                    }
                    else
                    {
                        CommonMethods.AddLog(true, $"写入变量True失败: {openSignalName}");
                    }
                }
                else if (action == "Close")
                {
                    // 关闭操作
                    if (CommonMethods.CommonWrite(closeSignalName, "true").IsSuccess)
                    {
                        CommonMethods.CommonWrite(openSignalName, "false");
                        //同时写入控制阀
                        CommonMethods.CommonWrite(SignalState, "0");
                    }
                    else
                    {
                        CommonMethods.AddLog(true, $"写入变量False失败: {closeSignalName}");
                    }
                }
            }
        }
    }
}
